﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using Intel.Server.WCFLibrary;
using System.Data;

namespace Intel.Server.GuiPlugins
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码和配置文件中的类名“Service1”。
    public class PluginsServer : IPluginServer
    {
        static PluginsServer()
        {
            ImageDir = new DirectoryInfo("d:\\plugins\\image");
            if (!ImageDir.Exists)
                ImageDir.Create();
            PluginDir = new DirectoryInfo("d:\\plugins\\plugin");
            if (!PluginDir.Exists)
                PluginDir.Create();

        }
        public List<PluginInfo> GetPluginsList()
        {
            List<PluginInfo> list = new List<PluginInfo>();
            DataTable dt = Globle.Db.GetPluginInfo();
            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    var p = new PluginInfo();
                    p.AddTime = (DateTime)dr["dt"];
                    p.Author = (string)dr["author"];
                    p.Uuid = (int)dr["uuid"];
                    p.DetailInfo = (string)dr["detail"];
                    p.Name = (string)dr["name"];
                    if(!(bool)(dr["isgui"]))
                        p.Type=PluginType.BackGroud;
                    else
                        p.Type=PluginType.Gui;
                    list.Add(p);
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                
            }
            return list;
        }

        public Stream GetPic(int Uuid)
        {
            string path = Globle.Db.GetImagePath(Uuid);
            var files = ImageDir.GetFiles(path);
            if (files.Length == 0)
                return null;
            FileStream fs = new FileStream(files[0].FullName, FileMode.Open, FileAccess.Read);
            //System.Threading.Thread.Sleep(3000);
            return fs;
        }
        public Stream GetPlugin(int Uuid)
        {
            string path = Globle.Db.GetPath(Uuid);
            var files = PluginDir.GetFiles(path);
            if (files.Length == 0)
                return null;
            FileStream fs = new FileStream(files[0].FullName, FileMode.Open, FileAccess.Read);
            //System.Threading.Thread.Sleep(3000);
            return fs;
        }

        public static DirectoryInfo ImageDir;
        public static DirectoryInfo PluginDir;
    }
}
